/*
 Copyright (c) Whitemice Consulting, 2007, 2008, 2009
 Adam Tauno Williams <awilliam@whitemice.org>
 License: MIT/X11

 Permission is hereby granted, free of charge, to any person obtaining a copy
 of this software and associated documentation files (the "Software"), to deal
 in the Software without restriction, including without limitation the rights
 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 copies of the Software, and to permit persons to whom the Software is
 furnished to do so, subject to the following conditions:

 The above copyright notice and this permission notice shall be included in
 all copies or substantial portions of the Software.
 
 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 THE SOFTWARE.
*/
using System;
using System.Collections;
using Gtk;
using Gdk;

namespace Whitemice.Calendar
{
  /* Paints the header for the day strip */
  public class DayStripHeader : DrawingArea
  {
    DayStrip  strip;
    
    public DayStripHeader(DayStrip _strip) : base ()
    {
      strip = _strip;
    } /* end ctor */

    /* Respond with the size we require in order to paint the header */    
    protected override void OnSizeRequested (ref Requisition requisition)
    {
      /*
      Pango.Layout  layout;
      int           width, height;
      */
      
      requisition.Width = strip.Allocation.Width;
      
      /* 
      layout = new Pango.Layout(this.PangoContext);
      layout.FontDescription = Pango.FontDescription.FromString ("Tahoma 10");
      layout.SetText("1972-12-06\nTuesday");
      */
      /* TODO: Calculate how much height we need from a possibly variable font
         size based on the user's theme or style */
      requisition.Height = 30;
    } /* end OnSizeRequested */

    protected override bool OnExposeEvent (EventExpose ev)
    {
      base.OnExposeEvent (ev);
      
      Paint (ev.Area);
      return true;
    } /* end OnExposeEvent */
    
    /* A simple method to request the entire widget replace itself */
    public void Repaint()
    {
      this.QueueDraw();
    } /* end Repaint */
    
    /* Repaint a portion of the widget */
    protected void Paint(Gdk.Rectangle _rect)
    {
      Pango.Layout  layout;
      int           w, x;
      
      /* paint header */
      foreach(CalendarDay day in strip.Dates)
      {
        w = day.Rectangle.Width;
        x = day.Rectangle.X;
        layout = new Pango.Layout(this.PangoContext);
        layout.FontDescription = Pango.FontDescription.FromString ("Tahoma 10");
        layout.SetText(string.Format("{0}\n{1}", 
                          day.Date.ToString("yyyy-MM-dd"),
                          day.Date.DayOfWeek.ToString()));
        layout.Alignment = Pango.Alignment.Center;
        layout.Width = (w - (strip.Padding * 2));
        Style.PaintLayout(Style, 
                          GdkWindow,
                          StateType.Normal, 
                          true, 
                          new Gdk.Rectangle(x, 0, Allocation.Width, Allocation.Height), 
                          this, 
                          null,
                          (x + (w / 2)), 
                          strip.Padding, 
                          layout);
      } /* end foreach day */
    } /* end Paint */
  } /* end class */
} 